<template>
  <div class="app-container">
    <el-form class="base-search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="90">
      <!-- <el-form-item label="运营渠道" prop="businessChannel">
        <el-select v-model="queryParams.businessChannel" placeholder="请选择运营渠道" clearable>
          <el-option
            v-for="dict in business_channel"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item label="来源渠道" prop="channelCode">
        <el-select v-model="queryParams.channelCode" placeholder="请选择来源渠道" clearable>
          <el-option
            v-for="dict in channelList"
            :key="dict.channelCode"
            :label="dict.channelName"
            :value="dict.channelCode"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属公司" prop="companyId">
        <el-tree-select
          v-model="queryParams.companyId"
          :data="companyList"
          :render-after-expand="true"
          :props="treeProps"
          node-key="id"
          clearable check-strictly
        />
      </el-form-item>
      <el-form-item label="用户编号" prop="platformUserId">
        <el-input
          v-model="queryParams.platformUserId"
          placeholder="请输入用户编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付模式" prop="payMode">
        <el-select v-model="queryParams.payMode" placeholder="请选择支付模式" clearable>
          <el-option
            v-for="dict in plat_order_pay_mode"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="支付方式" prop="payChannel">
        <el-select clearable v-model="queryParams.payChannel" placeholder="请选择支付方式">
          <el-option
            v-for="item in plat_order_pay_channel"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="订单编号" prop="outTradeNo">
        <el-input
          v-model="queryParams.outTradeNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易流水号" prop="transactionId">
        <el-input
          v-model="queryParams.transactionId"
          placeholder="请输入交易流水号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交易时间" prop="tradeTime">
        <el-date-picker clearable
          v-model="searchDate"
          type="daterange"
          value-format="YYYY-MM-DD"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          @change="confirmSearchDate">
        </el-date-picker>
      </el-form-item>

      <!-- <el-form-item label="交易方向" prop="direction">
        <el-select v-model="queryParams.direction" placeholder="请选择交易方向" clearable>
          <el-option
            v-for="dict in plat_trade_serial_direction"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <!-- <el-form-item label="交易类型" prop="tradeType">
        <el-select v-model="queryParams.tradeType" placeholder="请选择交易类型" clearable>
          <el-option
            v-for="dict in plat_trade_serial_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="base-container mb-50px">
      <el-row :gutter="10" class="mb-10px">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['platuser:transaction:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table border v-loading="loading" :data="platTradeSerialList">
        <!-- <el-table-column label="运营渠道" align="center" prop="businessChannel">
          <template #default="scope">
            <dict-tag :options="business_channel" :value="scope.row.businessChannel"/>
          </template>
        </el-table-column> -->
        <el-table-column label="来源渠道" align="center" prop="channelCode" min-width="130" show-overflow-tooltip />
        <el-table-column label="用户编号" align="center" prop="platformUserId" min-width="150" show-overflow-tooltip />
        <el-table-column label="手机号码" align="center" prop="mobile" min-width="120" />
        <el-table-column label="支付模式" align="center" prop="payMode">
          <template #default="scope">
            <dict-tag :options="plat_order_pay_mode" :value="scope.row.payMode"/>
          </template>
        </el-table-column>
        <el-table-column label="支付方式" align="center" prop="payChannel">
          <template #default="scope">
            <dict-tag :options="plat_order_pay_channel" :value="scope.row.payChannel"/>
          </template>
        </el-table-column>
        <el-table-column label="交易金额（元）" align="center" prop="amount" width="120">
          <template #default="scope">
            <span>{{ formatAmount(scope.row.amount) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="所属公司" align="center" prop="companyId" min-width="150" show-overflow-tooltip />
        <el-table-column label="订单编号" align="center" prop="outTradeNo" min-width="150" show-overflow-tooltip />
        <el-table-column label="交易流水号" align="center" prop="transactionId" min-width="150" show-overflow-tooltip />
        <el-table-column label="交易时间" align="center" prop="createTime" width="180"></el-table-column>

        <!-- <el-table-column label="交易方向" align="center" prop="direction">
          <template #default="scope">
            <dict-tag :options="plat_trade_serial_direction" :value="scope.row.direction"/>
          </template>
        </el-table-column> -->
        <!-- <el-table-column label="交易类型" align="center" prop="tradeType">
          <template #default="scope">
            <dict-tag :options="plat_trade_serial_type" :value="scope.row.tradeType"/>
          </template>
        </el-table-column> -->
        <!-- <el-table-column label="业务场景" align="center" prop="bizType" /> -->
        <!-- <el-table-column label="业务场景描述" align="center" prop="bizDesc" width="150" show-overflow-tooltip /> -->
      </el-table>
    </div>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Trade">
import { listPlatTradeSerial, getPlatTradeSerial } from "@/api/order/trade"
import { listChannel } from "@/api/channel/platchannelinfo"
import { getAllCompanies } from '@/api/company/company'

const { proxy } = getCurrentInstance()
const { plat_trade_serial_direction, business_channel, plat_trade_serial_type, plat_order_pay_mode, plat_order_pay_channel } = proxy.useDict('plat_trade_serial_direction', 'business_channel', 'plat_trade_serial_type', 'plat_order_pay_mode', 'plat_order_pay_channel')

const platTradeSerialList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const channelList = ref([])
const searchDate = ref([])
const companyList = ref([])                 // 公司列表
const treeProps = {
  label: 'name',
  disabled: (data, node) => {
    return data.check == false
  }
}

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    businessChannel: null,
    channelCode: null,
    platformUserId: null,
    companyId: null,
    mobile: null,
    payMode: null,
    payChannel: null,
    transactionId: null,
    outTradeNo: null,
    createTime: null,
    direction: null,
    tradeType: null,
    extTen: 2,    // 支付成功
  }
})

const { queryParams } = toRefs(data)

const confirmSearchDate = (e) => {
  if (e && e.length) {
    queryParams.value.createTimeBeginTime = e[0]
    queryParams.value.createTimeEndTime = e[1]
  } else {
    queryParams.value.createTimeBeginTime = null
    queryParams.value.createTimeEndTime = null
  }
  handleQuery()
}

/** 公司列表 */
function getCompanyList() {
  getAllCompanies().then(res => {
    companyList.value = res.data || []
  })
}

/** 查询渠道列表 */
function getChannelList() {
  const params = {
    pageNum: 1,
    pageSize: 100
  }
  listChannel(params).then(response => {
    channelList.value = response.rows
  })
}

/** 查询交易流水列表 */
function getList() {
  loading.value = true
  let params = {
    ...queryParams.value,
    'params[createTimeBeginTime]': queryParams.value.createTimeBeginTime,
    'params[createTimeEndTime]': queryParams.value.createTimeEndTime,
  }
  delete params.createTimeBeginTime
  delete params.createTimeEndTime
  listPlatTradeSerial(params).then(response => {
    platTradeSerialList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  searchDate.value = []
  queryParams.value.createTimeBeginTime = null
  queryParams.value.createTimeEndTime = null
  proxy.resetForm("queryRef")
  handleQuery()
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('platuser/transaction/export', {
    ...queryParams.value,
    exportType: 'kfdd'
  }, `扣费订单_${proxy.getFileImportName()}.xlsx`)
}

getCompanyList()
getChannelList()
getList()
</script>
